/**
 * Project Name:springmgw Maven Webapp
 * File Name:MessageService.java
 * Package Name:com.geng.imr.service
 * Date:2015年3月18日下午1:22:43
 * Copyright (c) 2015, 上海理想信息产业（集团） All Rights Reserved.
 *
*/

package com.geng.imr.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.geng.imr.model.NewsItem;
import com.geng.spring4.model.ReceiveMessage;
import com.geng.spring4.model.ReceiveMessage.MsgType;
import com.geng.spring4.util.FormatXmlProcess;
import com.geng.spring4.util.TulingApiProcess;

/**
 * ClassName:MessageService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年3月18日 下午1:22:43 <br/>
 * @author   GengHongChuang
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService{
    
    public String deal(ReceiveMessage message){
        String result = "";
        if(MsgType.event == message.getMsgType()){
            //关注事件
            if("subscribe".equals(message.getEventType())){
                result = tranText("感谢您关注篮球吧，现在篮球精灵机器人已启用，其他功能陆续开放中，敬请期待...", message);  
            }
            //click事件
            if("CLICK".equals(message.getEventType())){
                if("xljl".equals(message.getEventId())){
                    result = tranText("你好，我是小篮精灵，我好寂寞陪我聊聊天吧！", message);  
                }
                if("zbb".equals(message.getEventId())){
                    List<NewsItem> items = new ArrayList<NewsItem>();
                    NewsItem item = new NewsItem("直播吧", "比赛直播地址，建议使用wifi观看比赛！", "http://springmgw.oschina.mopaas.com/img/banner.png", "http://m.zhibo8.cc/");
                    items.add(item);
                    result=tranNews(items, message);
                }
            }
            System.out.println(message.getEventType());
        }else if(MsgType.text == message.getMsgType()){
            String tulingContent = new TulingApiProcess().getTulingResult(message.getContent());  
            result = tranText(tulingContent, message);
        }
        return result;
      
    }
    public String tranText(String content,ReceiveMessage message){
        return new FormatXmlProcess().formatXmlAnswer(message.getOpenId(),message.getToUser(), content);
    }
    public String tranNews(List<NewsItem> newsItems,ReceiveMessage message){
        return new FormatXmlProcess().formatXmlNewsAnswer(message.getOpenId(), message.getToUser(), newsItems);
    }

}
